package com.atxiao.service.JUC;

import lombok.Data;

@Data
class Ticket{
    private int ticketNumber=100;
    public synchronized void saleTicket(){
        if (ticketNumber>0){
//            System.out.println(Thread.currentThread().getName()+"卖出了第"+ticketNumber--+"张票，还剩下"+(ticketNumber)+"张票");
//            ticketNumber--;

            System.out.println(Thread.currentThread().getName()+"卖出了第"+ticketNumber+"张票,还剩下"+(--ticketNumber));
//            ticketNumber--;
        }
        }
}

public class TicketService {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        Thread threadAA = new Thread(() -> {

        for (int i = 0; i < 1000; i++) {
            ticket.saleTicket();
        }
        }, "AA线程");


        Thread threadBB = new Thread(() -> {

            for (int i = 0; i < 1000; i++) {
                ticket.saleTicket();
            }
        }, "BB线程");


        Thread threadCC = new Thread(() -> {

            for (int i = 0; i < 1000; i++) {
                ticket.saleTicket();
            }
        }, "CC线程");

        threadAA.start();
        threadBB.start();
        threadCC.start();


    }
}
